package com.coupons.business.merchantoffers.dao;

import java.util.ArrayList;
import java.util.List;

import com.coupons.business.common.exception.BaseCouponsAppException;
import com.coupons.business.common.util.Helper;
import com.coupons.business.common.vo.MerchantOffer;
import com.coupons.business.common.vo.decorator.MerchantOffersRowMapper;
import com.coupons.business.util.BaseUtilDAO;

public class MerchantOffersDAO extends BaseUtilDAO implements
		IMerchantOffersDAO {

	/*
	 * MERCHANTOFFERS_INSERT_STATEMENT =
	 * "INSERT INTO MERCHANTOFFERS(OFFER_ID,OFFER_MORE_TXT,OFFER_MSG,EXPIRY_DATE,"
	 * +
	 * "COUPON_CODE,CTGRY_ID,HOME_PG_OFFER,HOME_PG_OOFER_INDEX,SPECIAL_OOFER,SPECIAL_OFFER_NDEX,"
	 * + "OFFER_REDIRECT_URL,OFFER_INDEX,LAST_UPDT_USER,PARTNER_ID,MRCHNT_ID" +
	 * "CRTN_DT,LAST_UPDT_DT) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,SYSDATE,SYSDATE)"
	 * ;
	 */
	public void createMerchantOffers(MerchantOffer objaMerchantOffers)
			throws BaseCouponsAppException {
		int merchantoffersId = getSequenceNextValue(SEQUENCE_NAME);
		Object objParams[] = new Object[] { new Integer(merchantoffersId),
				objaMerchantOffers.getMerchantMoreOffersText(),
				objaMerchantOffers.getOfferMessage(),
				objaMerchantOffers.getExpiryDate(),
				objaMerchantOffers.getCouponCode(),
				new Integer(objaMerchantOffers.getCategoryId()),
				objaMerchantOffers.getHomePageOffer(),
				new Integer(objaMerchantOffers.getHomePageOfferIndex()),
				objaMerchantOffers.getSpecialOffer(),
				new Integer(objaMerchantOffers.getSpecialOfferIndex()),
				objaMerchantOffers.getOfferRedirectURL(),
				new Integer(objaMerchantOffers.getMerchantPageOfferIndex()),
				objaMerchantOffers.getLastUpdatedUser(),
				new Integer(objaMerchantOffers.getPartnerId()),
				new Integer(objaMerchantOffers.getMerchantId()) };

		int rtnVal = this.saveOrUpdateOrDelete(
				MERCHANTOFFERS_INSERT_STATEMENT.toString(), objParams);
		objaMerchantOffers.setMerchantId(merchantoffersId);
		System.out.println("executed dao");
		if (rtnVal <= 0) {
			System.out
					.println("Unable to insert record into MerchantOffers table");
			return;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.coupons.business.merchant.dao.IMerchantDAO#updateMerchant(com.coupons
	 * .business.common.vo.Merchant)
	 * 
	 * "UPDATE MERCHANTOFFERS SET OFFER_MORE_TXT = ?, OFFER_MSG = ?, EXPIRY_DATE = ?, "
	 * +
	 * "COUPON_CODE = ?, HOME_PG_OFFER=?, HOME_PG_OOFER_INDEX=?, SPECIAL_OOFER=?, "
	 * + " SPECIAL_OFFER_NDEX=?, " +" OFFER_REDIRECT_URL=?, OFFER_INDEX=?, " +
	 * "LAST_UPDT_USER=?, LAST_UPDT_DT=SYSDATE WHERE OFFER_ID = ?";
	 */
	public void updateMerchantOffers(MerchantOffer objaMerchantOffers)
			throws BaseCouponsAppException {
		Object objParams[] = new Object[] {

		objaMerchantOffers.getMerchantMoreOffersText(),
				objaMerchantOffers.getOfferMessage(),
				objaMerchantOffers.getExpiryDate(),
				objaMerchantOffers.getCouponCode(),
				objaMerchantOffers.getHomePageOffer(),
				objaMerchantOffers.getHomePageOfferIndex(),
				objaMerchantOffers.getSpecialOffer(),
				objaMerchantOffers.getSpecialOfferIndex(),
				objaMerchantOffers.getOfferRedirectURL(),
				objaMerchantOffers.getMerchantPageOfferIndex(),
				objaMerchantOffers.getLastUpdatedUser(),
				new Integer(objaMerchantOffers.getOfferId()) };
		int rtnVal = this.saveOrUpdateOrDelete(
				MERCHANTOFFERS_UPDATE_STATEMENT.toString(), objParams);
		System.out.println("executed dao");
		if (rtnVal <= 0) {
			System.out
					.println("Unable to insert record into MerchantOffers table");
			return;
		}
	}

	/*
	 * 
	 * 
	 * 
	 * SELECT * FROM MERCHANT_OFFERS where OFFER_ID = ?
	 */
	public MerchantOffer getMerchantOffers(int merchantoffersId)
			throws BaseCouponsAppException {
		try {
			Object objParams[] = new Object[] { new Integer(merchantoffersId) };
			List<Object> list = this.findRecords(GET_MERCHANTOFFERS_STATEMENT,
					objParams, new MerchantOffersRowMapper());

			if (null != list && list.size() > 0) {
				return (MerchantOffer) list.get(0);
			} else
				return null;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getAllMerchants()
	 * SELECT * FROM MERCHANT
	 */
	public List<MerchantOffer> getAllMerchantOffers()
			throws BaseCouponsAppException {
		try {
			List list = this.findRecords(GET_ALL_MERCHANTOFFERS_STATEMENT,
					new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchantoffers.dao.IMerchantOffersDAO#getOffersByMerchant(int)
	 * SELECT * FROM MERCHANT_OFFERS where MERCHANT_ID = ?
	 */
	public List<MerchantOffer> getOffersByMerchant(int merchantId)
			throws BaseCouponsAppException {
		
		try {
			Object objParams[] = new Object[] { new Integer(merchantId) };
			List list = this.findRecords(GET_OFFERS_BY_MERCHANT_STATEMENT,
					objParams, new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchantoffers.dao.IMerchantOffersDAO#getOffersByCategory(int)
	 * SELECT * FROM MERCHANT_OFFERS where CTGRY_ID = ?
	 */
	public List<MerchantOffer> getOffersByCategory(int partnerId, String categoryName)throws BaseCouponsAppException{
		try {
			Object objParams[] = new Object[] { new Integer(partnerId),categoryName};
			List list = this.findRecords(GET_OFFERS_BY_CATEGORY_STATEMENT,
					objParams, new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchantoffers.dao.IMerchantOffersDAO#getHomePageOffers()
	 * SELECT * FROM MERCHANT_OFFERS WHERE HOME_PG_OFFER = 'yes'
	 */
	public List<MerchantOffer> getHomePageOffers(int partnerId)throws BaseCouponsAppException 
	{
		try {
			Object objParams[] = new Object[] { new Integer(partnerId) };
			List list = this.findRecords(GET_HOME_PAGE_OFFERS_STATEMENT,
					objParams,new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchantoffers.dao.IMerchantOffersDAO#getSpecialPageOffers()
	 * SELECT * FROM MERCHANT_OFFERS WHERE SPECIAL_OOFER = 'yes'
	 */
	public List<MerchantOffer> getSpecialPageOffers(int partnerId)throws BaseCouponsAppException
	{
		try {
			Object objParams[] = new Object[] { new Integer(partnerId) };
			List list = this.findRecords(GET_SPECIAL_PAGE_OFFERS_STATEMENT,
					objParams,new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<MerchantOffer> getOffersBySearchKey(int partnerId, String searchKey)throws BaseCouponsAppException
	{
		try {
			searchKey = Helper.prepareValidkeyWord(searchKey);
			searchKey=searchKey.toUpperCase();
			Object objParams[] = new Object[] { new Integer(partnerId)};
			String query = GET_OFFERS_BY_SEARCHKEY+searchKey+
				GET_OFFERS_BY_SEARCHKEY2+searchKey+GET_OFFERS_BY_SEARCHKEY3+GET_OFFERS_BY_SEARCHKEY4+
				searchKey+GET_OFFERS_BY_SEARCHKEY5;
			System.out.println("query: "+query);
			List list = this.findRecords(query,	objParams, new MerchantOffersRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {
			e.printStackTrace();
			return new ArrayList<MerchantOffer>();
		}
	}
	
}
